package com.groupbuying.currency.producer.controller;

import com.groupbuying.currency.producer.domain.CurrencyCustomerDO;
import com.groupbuying.currency.producer.service.CurrencyCustomerService;
import com.groupbuying.currency.producer.utils.ObjectUtils;
import com.groupbuying.currency.producer.utils.PageUtils;
import com.groupbuying.currency.producer.utils.Query;
import com.groupbuying.currency.producer.utils.R;
import com.groupbuying.currency.producer.utils.apiresult.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 积分会员表
 * 
 * @author ywxd
 * @email yuwenxiaodong@126.com
 * @date 2019-04-15 17:20:51
 */
 
@RestController
@RequestMapping("/account")
public class CurrencyCustomerController {
	@Autowired
	private CurrencyCustomerService currencyCustomerService;

	/**
	 * class_name: CurrencyCustomerController
	 * package: com.groupbuying.currency.controller
	 * description: 创建用户碎银账户
	 * creat_user: Ywxd
	 * creat_date: 2019/4/15
	 * creat_time: 17:36
	 **/

	@PostMapping("/create")
	public Map createCurrencyCustomer(@RequestParam Map<String, Object> params){
		String customerId = params.get("customerId").toString();
		return ObjectUtils.objectToMap(currencyCustomerService.createCurrencyCustomer(customerId));
    }

	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CurrencyCustomerDO> currencyCustomerList = currencyCustomerService.list(query);
		int total = currencyCustomerService.count(query);
		PageUtils pageUtils = new PageUtils(currencyCustomerList, total);
		return pageUtils;
	}

	/**
	 * 获取积分账户
	 */
	@GetMapping("/getInfo")
	public Map getInfo(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<CurrencyCustomerDO> currencyCustomerList = currencyCustomerService.list(query);
		if(currencyCustomerList.size() == 0) {
			return null;
		}
		CurrencyCustomerDO currencyCustomerDO = currencyCustomerList.get(0);
		return ObjectUtils.objectToMap(currencyCustomerDO);
	}


	/**
	 * 修改积分支付密码
	 */
	@PostMapping("/changePayPwd")
	public ApiResult changePayPwd(@RequestParam Map<String, Object> params){
		return currencyCustomerService.changeCurrencyPwd(params);
	}


	/**
	 * 保存
	 */
	@PostMapping("/save")
	public R save( CurrencyCustomerDO currencyCustomer){
		if(currencyCustomerService.save(currencyCustomer)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update( CurrencyCustomerDO currencyCustomer){
		currencyCustomerService.update(currencyCustomer);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( String customerId){
		if(currencyCustomerService.remove(customerId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") String[] customerIds){
		currencyCustomerService.batchRemove(customerIds);
		return R.ok();
	}
	
}
